package com.ebelter.temperaturegun.model.http;


import android.os.SystemClock;

import com.ebelter.btcomlib.models.bluetooth.products.temperature2.bean.TemMesureResult2;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperaturegun.model.app.TemUtils;
import com.ebelter.temperaturegun.ui.activity.MainActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.Queue;

public class TgUpLoadDataUtil {
    private static final String TAG = "TgUpLoadDataUtil";
    private static TgUpLoadDataUtil instance;
    public static final int UPLOADSUCCESS = 97;



    public static TgUpLoadDataUtil getInstance() {
        if (instance==null){
            synchronized (TgUpLoadDataUtil.class){
                if (instance==null) instance = new TgUpLoadDataUtil();
            }
        }
        return instance;
    }

    private Queue<TemMesureResult2> uploadList ;
    private UploadThread mUploadThread;

    private TgUpLoadDataUtil() {
        uploadList = new LinkedList<>();
        mUploadThread = new UploadThread();
        mUploadThread.isLiveFlag=true;
    }

    public void start(){
        if (mUploadThread!=null&&!mUploadThread.isAlive()){
            if (mUploadThread==null){
                mUploadThread=new UploadThread();
                mUploadThread.isLiveFlag=true;
            }
            mUploadThread.start();
        }
    }

    public void stop(){
        if (mUploadThread!=null&&mUploadThread.isAlive()){
            mUploadThread.isLiveFlag=false;
            mUploadThread.interrupt();
            mUploadThread=null;
        }
    }

    public void add(TemMesureResult2 t2){
        if (uploadList!=null&&t2!=null){
            uploadList.add(t2);
        }

    }

    class UploadThread extends Thread{
        private long sleepTime = 6000;
        public boolean isLiveFlag = true;
        @Override
        public void run() {
            super.run();


            while (isLiveFlag){
                LogUtils.i(TAG,"---UploadThread--正在运行工作");
                if (uploadList!=null&&uploadList.size()>0){
                    final TemMesureResult2 t2 = uploadList.poll();
                    long userId = TemUtils.getUerID();

                    //00：耳温C标记
                    //01：耳温F标记
                    //02：额温C标记
                    //03：额温F标记
                    if (t2!=null&&userId>0){
                        int tempType;//  //tempType:温度类型(1:耳温,2:额温),不传默认耳温
                        if (t2.com==0||t2.com==1){
                            tempType=1;
                        }else {
                            tempType=2;
                        }


                        TGNetUtils.getInstance().uploadTem(t2, userId, t2.temperature,tempType, t2.measureTimeStr,
                                new HttpResponse<BaseResponse>() {
                            @Override
                            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                                if (isRequestSuccess && baseResponse != null && baseResponse.resultCode == 0){
                                    LogUtils.i(TAG,"TgUpLoadDataUtil 数据上传 成功 " + t2.toString());

                                    CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,UPLOADSUCCESS,null);
                                    EventBus.getDefault().post(commonEventBus);

                                }else {
                                    LogUtils.i(TAG,"TgUpLoadDataUtil 数据上传 失败 " + t2.toString());
                                    uploadList.add(t2);
                                }
                            }
                        });
                    }
                    if(uploadList.size() > 0){
                        sleepTime = 3000;
                    }else {
                        sleepTime = 6000;
                    }
                }


                SystemClock.sleep(sleepTime);
            }



        }
    }
}
